home *** CD-ROM | disk | FTP | other *** search
-
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
-
- #include "lotto_data_class.h"
-
-
- FILE *fp;
-
- lotto_data::lotto_data()
- {
- }
-
- lotto_data::~lotto_data()
- {
- }
-
- bool lotto_data::check_for_file( void )
- {
- if( (fp = fopen( "lotto.dat", "rb")) == NULL )
- {
- fclose( fp );
- return false;
- } else {
- fclose( fp );
- return true;
- }
- }
-
- bool lotto_data::set_struct_file( void )
- {
- char temp_buf[5] = {NULL};
- int buf_count = 0;
- int set_count = 0;
-
- record[number_of_records] = new lotto_struct;
-
- for( int index = 0; index < 20; index++ )
- {
- if( isdigit( user_input[index] ) != 0 ) // test if we have a digit 0 - 9
- {
- temp_buf[buf_count] = user_input[index];
- buf_count++; // increment the buffer
- }
-
- switch( user_input[index] )
- {
- case '.':
- {
- record[number_of_records]->set[set_count] = ( atoi( temp_buf ) );
-
- set_count++;
-
- for( int index2 = 0; index2 < 5; index2++ )
- {
- temp_buf[index2] = 0;
- }
-
- buf_count = 0;
- break;
- }
-
- case 'q': case 'Q': // user wants to quit
- {
- delete( record[number_of_records] );
- lotto_data::clear_user_input();
- return false;
- }
-
- default:
- {
- if( set_count == 5 ) // then this is the last number
- {
- if( user_input[index] == '.' ) // if it is a dot
- {
- index++; // move the index up one
- }
-
- for( int count = 0; count < 5; count++ )
- {
- temp_buf[count] = 0; // zero out the buffer
- }
- buf_count = 0;
-
- while( ((user_input[index]) != 0) && (index < 20) )
- {
- temp_buf[buf_count] = user_input[index];
- index++;
- buf_count++;
- }
-
- record[number_of_records]->set[set_count] = ( atoi( temp_buf ) );
- bubble_sort_set( number_of_records );
- lotto_data::clear_user_input();
- number_of_records++;
- return true;
- } else {
- break;
- }
- }
- }
- }
- // if something went wrong...
- delete( record[number_of_records] );
- lotto_data::clear_user_input();
- return false;
- }
-
- bool lotto_data::save_whole_file( void )
- {
- lotto_struct *current = NULL;
-
- if( (fp = fopen( "lotto.dat", "wb+" )) == NULL )
- {
- return false;
- } else {
- for( int index = 0; index < number_of_records; index++ )
- {
- current = NULL;
-
- if( index == 0 )
- {
- current = NULL;
- record[index]->prev = current;
- } else {
- current = record[index - 1];
- record[index]->prev = current;
- }
- if( index == number_of_records )
- {
- current = NULL;
- record[index]->next = current;
- } else {
- current = record[index + 1];
- record[index]->next = current;
- }
- lotto_data::get_stats( index );
- fwrite( record[index], (sizeof( lotto_struct )), 1, fp );
- }
-
- }
- fclose( fp );
- lotto_data::clear_records();
- number_of_records = 0; // done only AFTER clear_records() !
- return true;
- }
-
- void lotto_data::simple_save( void )
- {
- fp = fopen( "lotto.dat", "wb+" );
- for( int pass = 0; pass < number_of_records; pass++ )
- {
- fwrite( record[pass], (sizeof( lotto_struct )), 1, fp );
- }
- fclose( fp );
- }
-
- void lotto_data::clear_records( void )
- {
- for( int index = 0; index < number_of_records; index++ )
- {
- if( record[number_of_records] )
- {
- delete( record[number_of_records] );
- }
- }
- }
-
-
- void lotto_data::get_stats( int num )
- {
- record[num]->evens = 0;
- record[num]->odds = 0;
- record[num]->reds = 0;
- record[num]->whites = 0;
- record[num]->blues = 0;
-
- for( int index = 0; index < 6; index++ )
- {
- // odds & evens
- if( (record[num]->set[index] % 2) == 0 )
- {
- record[num]->evens += 1;
- } else {
- record[num]->odds += 1;
- }
-
- // reds, whites & blues
- if( (record[num]->set[index] > 0) && (record[num]->set[index] < 18) )
- {
- record[num]->reds += 1;
- }
- if( (record[num]->set[index] > 17) && (record[num]->set[index] < 35) )
- {
- record[num]->whites += 1;
- }
- if( (record[num]->set[index] > 34) && (record[num]->set[index] < 52) )
- {
- record[num]->blues += 1;
- }
- }
- }
-
-
- // bubble sort for a set
- void lotto_data::bubble_sort_set( int rec_num )
- {
- int size = 6;
- int hold = 0;
-
- // the actual bubble sort
- for( int pass = 1; pass <= (size - 1); pass++ )
- {
- for( int pass2 = 0; pass2 <= (size - 2); pass2++ )
- {
- if( record[rec_num]->set[pass2] > record[rec_num]->set[pass2 + 1] )
- {
- hold = record[rec_num]->set[pass2];
- record[rec_num]->set[pass2] = record[rec_num]->set[pass2 + 1];
- record[rec_num]->set[pass2 + 1] = hold;
- }
- }
- }
- }
-
-
- void lotto_data::clear_user_input( void )
- {
- for( int index = 0; index < 20; index++ )
- {
- user_input[index] = 0;
- }
- }
-
- bool lotto_data::read_file( void )
- {
- if( (fp = fopen( "lotto.dat", "rb" )) == NULL )
- {
- fclose( fp );
- return false;
- } else {
- number_of_records = 0;
-
- for( int index = 0; index < MAX_STRUCTS; index++ )
- {
- record[index] = new lotto_struct;
- record[index]->prev = NULL;
- record[index]->next = NULL;
-
- fread( record[index], sizeof( lotto_struct ), 1, fp );
-
- if( fp == NULL)
- {
- fclose( fp );
- return false;
- }
- if( record[index]->next != NULL )
- {
- number_of_records++;
- }
- if( record[index]->next == NULL )
- {
- fclose( fp );
- return true;
- }
- }
- }
- fclose( fp );
- return false;
- }
-
-
- bool lotto_data::update_struct_file( void )
- {
- char temp_buf[5] = {NULL};
- int buf_count = 0;
- int set_count = 0;
-
- lotto_struct *temp_array[MAX_STRUCTS];
-
- for(int ind = 0; ind < 20; ind++ )
- {
- if( (user_input[ind] == 'q') || (user_input[ind] == 'Q') )
- {
- return false;
- }
- }
-
- record[number_of_records + 1] = new lotto_struct;
- temp_array[0] = new lotto_struct;
- for(int pass = 0; pass < number_of_records; pass++ )
- {
- temp_array[pass + 1] = new lotto_struct;
- temp_array[pass + 1] = record[pass];
- }
-
-
-
- for( int index = 0; index < 20; index++ )
- {
- if( isdigit( user_input[index] ) != 0 ) // test if we have a digit 0 - 9
- {
- temp_buf[buf_count] = user_input[index];
- buf_count++; // increment the buffer
- }
-
- switch( user_input[index] )
- {
- case '.':
- {
- temp_array[0]->set[set_count] = ( atoi( temp_buf ) );
-
- set_count++;
-
- for( int index2 = 0; index2 < 5; index2++ )
- {
- temp_buf[index2] = 0;
- }
-
- buf_count = 0;
- break;
- }
-
- case 'q': case 'Q': // user wants to quit
- {
- for( int index3 = 1; index3 < (number_of_records + 1); index3++ )
- {
- record[index3] = temp_array[index];
- }
- lotto_data::clear_user_input();
- return false;
- }
-
- default:
- {
- if( set_count == 5 ) // then this is the last number
- {
- if( user_input[index] == '.' ) // if it is a dot
- {
- index++; // move the index up one
- }
-
- for( int count = 0; count < 5; count++ )
- {
- temp_buf[count] = 0; // zero out the buffer
- }
- buf_count = 0;
-
- while( ((user_input[index]) != 0) && (index < 20) )
- {
- temp_buf[buf_count] = user_input[index];
- index++;
- buf_count++;
- }
-
- temp_array[0]->set[set_count] = ( atoi( temp_buf ) );
-
- for( int index4 = 0; index4 < (number_of_records + 1); index4++ )
- {
- record[index4] = temp_array[index4];
- }
-
- bubble_sort_set( number_of_records );
- lotto_data::clear_user_input();
- number_of_records++;
- return true;
- } else {
- break;
- }
- }
- }
- }
- // if something went wrong...
- delete( record[number_of_records] );
- lotto_data::clear_user_input();
- return false;
- }
-
- bool lotto_data::edit_struct_file( void )
- {
- char temp_buf[5] = {NULL};
- int buf_count = 0;
- int set_count = 0;
- int total = 0;
-
- lotto_struct *temp_array[MAX_STRUCTS];
-
- for(int ind = 0; ind < 20; ind++ )
- {
- if( (user_input[ind] == 'q') || (user_input[ind] == 'Q') )
- {
- return false;
- }
- }
-
- record[number_of_records + 1] = new lotto_struct;
-
-
- for(int pass = 0; pass < (number_of_records + 1); pass++ )
- {
- temp_array[pass] = new lotto_struct;
- }
-
- for( int pass2 = 0; pass2 <= (number_of_records + 1); pass2++ )
- {
- if( pass2 == (user_request - 1))
- {
- temp_array[pass2 + 1] = record[pass2];
- pass2++;
- } else {
- temp_array[pass2] = record[pass2];
- }
-
- }
-
-
- for( int index = 0; index < 20; index++ )
- {
- if( isdigit( user_input[index] ) != 0 ) // test if we have a digit 0 - 9
- {
- temp_buf[buf_count] = user_input[index];
- buf_count++; // increment the buffer
- }
-
- switch( user_input[index] )
- {
- case '.':
- {
- temp_array[user_request - 1]->set[set_count] = ( atoi( temp_buf ) );
-
- set_count++;
-
- for( int index2 = 0; index2 < 5; index2++ )
- {
- temp_buf[index2] = 0;
- }
-
- buf_count = 0;
- break;
- }
-
- case 'q': case 'Q': // user wants to quit
- {
- for( int index3 = 1; index3 < (number_of_records + 1); index3++ )
- {
- record[index3] = temp_array[index3];
- }
- lotto_data::clear_user_input();
- return false;
- }
-
- default:
- {
- if( set_count == 5 ) // then this is the last number
- {
- if( user_input[index] == '.' ) // if it is a dot
- {
- index++; // move the index up one
- }
-
- for( int count = 0; count < 5; count++ )
- {
- temp_buf[count] = 0; // zero out the buffer
- }
- buf_count = 0;
-
- while( ((user_input[index]) != 0) && (index < 20) )
- {
- temp_buf[buf_count] = user_input[index];
- index++;
- buf_count++;
- }
-
- temp_array[user_request - 1]->set[set_count] = ( atoi( temp_buf ) );
-
- total = (number_of_records + 1);
- number_of_records = 0;
-
- for( int index4 = 0; index4 < total; index4++ )
- {
- record[index4] = temp_array[index4];
- number_of_records++;
- }
-
- bubble_sort_set( number_of_records );
- lotto_data::clear_user_input();
- return true;
- } else {
- break;
- }
- }
- }
- }
-
- // if something went wrong...
- delete( record[number_of_records] );
- lotto_data::clear_user_input();
- return false;
- }